<!DOCTYPE html>
<html class="ui-page-login">

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<link href="css/mui.min.css" rel="stylesheet" />
		<link href="css/style.css" rel="stylesheet" />
		<style>
			.mui-input-group {
				margin-top: 10px;
			}
			
			.mui-input-group label {
				width: 22%;
			}
			
			.mui-input-row label~input,
			.mui-input-row label~select,
			.mui-input-row label~textarea {
				width: 78%;
				border: none;
			}
			/*.mui-checkbox input[type=checkbox],
			.mui-radio input[type=radio] {
				top: 6px;
			}*/
			
			.mui-content-padded {
				margin-top: 25px;
			}
			
			.mui-btn {
				padding: 10px;
				width: 28%;
				border-radius: 15px;
				margin-left: 5%;
				margin-right: 5%;
			}
			
			.oauth-area {
				position: absolute;
				bottom: 20px;
				left: 0px;
				text-align: center;
				width: 100%;
				padding: 0px;
				margin: 0px;
			}
			
			.oauth-area .oauth-btn {
				display: inline-block;
				width: 50px;
				height: 50px;
				background-position: center center;
				background-repeat: no-repeat;
				margin: 0px 20px;
				border: solid 1px #ddd;
				border-radius: 25px;
			}
			
			.oauth-area .oauth-btn:active {
				border: solid 1px #aaa;
			}
			
			.oauth-area .oauth-btn.disabled {
				background-color: #ddd;
			}
			
			#logoImages {
				height: 30%;
				width: 100%;
				vertical-align: middle;
				text-align: center;
			}
			
			#logoImage {
				height: 100%;
				width: 80%;
			}
			
			.mui-button-row {
				margin-top: 5%;
			}
		</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<h1 class="mui-title">登录</h1>
		</header>
		<div class="mui-content">
			<div id="logoImages">
				<img id="logoImage" src="images/shtoone_logo.png" />
			</div>
			<form id='login-form' class="mui-input-group">
				<div class="mui-input-row">
					<label>账号</label>
					<input id='account' type="text" class="mui-input-clear mui-input" placeholder="请输入账号" value="">
				</div>
				<div class="mui-input-row">
					<label>密码</label>
					<input id='password' type="password" class="mui-input-clear mui-input" placeholder="请输入密码" value="">
				</div>
			</form>
			<form class="mui-input-group">
				<ul class="mui-table-view mui-table-view-chevron">
					<li class="mui-table-view-cell">
						自动登录
						<div id="autoLogin" class="mui-switch">
							<div class="mui-switch-handle"></div>
						</div>
					</li>
				</ul>
			</form>
			<div class="mui-button-row">
				<button id='login' type="button" class="mui-btn mui-btn-outlined">登录</button>
				<button type="button" class="mui-btn mui-btn-outlined">退出</button>
			</div>
		</div>
		<div class="mui-content-padded oauth-area">
			上海同望信息技术有限公司
		</div>
		<script src="js/mui.min.js"></script>
		<script src="js/mui.enterfocus.js"></script>
		<script src="js/app.js"></script>
		<script src="js/global.js"></script>
		<script>
			(function($, doc) {
				$.init({
					statusBarBackground: '#f7f7f7'
				});
				$.plusReady(function() {
					initPush();
					plus.screen.lockOrientation("portrait-primary");
					checked();
					var settings = app.getSettings();
					var state = app.getState();
					var mainPage = plus.webview.getWebviewById("main");
					var main_loaded_flag = false;
					if(!mainPage) {
						mainPage = $.preload({
							"id": 'main',
							"url": 'main.html'
						});
					} else {
						main_loaded_flag = true;
					}

					mainPage.addEventListener("loaded", function() {
						main_loaded_flag = true;
					});
					var toMain = function() {
						$.fire(mainPage, 'show', null);
						setTimeout(function() {
							$.openWindow({
								url: 'main.html',
								id: 'main',
								show: {
									aniShow: 'pop-in'
								},
								waiting: {
									autoShow: true, //自动显示等待框，默认为true  
									title: '正在加载...' //等待对话框上显示的提示内容  
								}
							});
						}, 0);
					};
					//检查 "登录状态/锁屏状态" 开始
					if(settings.autoLogin && state.token && settings.gestures) {
						$.openWindow({
							url: 'unlock.html',
							id: 'unlock',
							show: {
								aniShow: 'pop-in'
							},
							waiting: {
								autoShow: false
							}
						});
					} else if(settings.autoLogin && state.token) {
						toMain();
					}
					// close splash
					setTimeout(function() {
						//关闭 splash
						plus.navigator.closeSplashscreen();
					}, 600);
					//检查 "登录状态/锁屏状态" 结束
					var loginButton = doc.getElementById('login');
					var accountBox = doc.getElementById('account');
					var passwordBox = doc.getElementById('password');
					var autoLoginButton = doc.getElementById("autoLogin");
					loginButton.addEventListener('tap', function(event) {
						var loginInfo = {
							account: accountBox.value,
							password: passwordBox.value
						};
						app.login(loginInfo, function(err) {
							if(err) {
								plus.nativeUI.toast(err);
								return;
							}
							toMain();
						});
					});
					$.enterfocus('#login-form input', function() {
						$.trigger(loginButton, 'tap');
					});
					
					autoLoginButton.classList[settings.autoLogin ? 'add' : 'remove']('mui-active')
					autoLoginButton.addEventListener('toggle', function(event) {
						setTimeout(function() {
							var isActive = event.detail.isActive;
							settings.autoLogin = isActive;
							app.setSettings(settings);
						}, 50);
					}, false);
					
					var backButtonPress = 0;
					$.back = function(event) {
						backButtonPress++;
						if(backButtonPress > 1) {
							plus.runtime.quit();
						} else {
							plus.nativeUI.toast('再按一次退出应用');
						}
						setTimeout(function() {
							backButtonPress = 0;
						}, 1000);
						return false;
					};
				});
			}(mui, document));
			function initPush() {
//				document.addEventListener('plusready', function() {
					// 在这里调用5+ API 
					var pushState = plus.storage.getItem("pushState");
					console.log("login-pushState--" + pushState);
					if(pushState==null){
						plus.storage.setItem("pushState", "open");
					}
					if(pushState == "close") {
						pushDisable();
					} else {
						pushEnable();
					}
//				}, false);
			}
			
			
			var mVersionName;
			var mDescrition;
			var mCode;
			var mFileName;

			function checked() {
				mui.getJSON("manifest.json", null, function(data) {
					mCode = data.version.code;
				});

				mui.ajax(UPDATE_URL, {
					dataType: 'json', //服务器返回json格式数据
					type: 'get', //HTTP请求类型
					timeout: 10000, //超时时间设置为10秒
					headers: {
						'Content-Type': 'text/html'
					},
					success: function(data) {
						//console.log(JSON.stringify(data));
						var code = data.obj.data[0].version_code;
						mVersionName = data.obj.data[0].version_name;
						mDescrition = data.obj.data[0].desctition;
						mFileName = data.obj.data[0].file_name;
						if(parseInt(code) > parseInt(mCode)) {
							updateDIALOG();
						} 
//						else {
//							mui.toast("目前已经是最新版本！");
//						}
					},
					error: function(xhr, type, errorThrown) {
						//异常处理；
						console.log(type);
						return callback('接口正在维护中');
					}
				});
			}

			function updateDIALOG() {
				console.log("执行下载更新");
				var btnArray = ['否', '是'];
				console.log(mDescrition);
				mui.confirm(mDescrition, '发现新版本:' + mVersionName, btnArray, function(e) {
					if(e.index == 1) {
						console.log("确认更新");
						downWgt();
					} else {
						console.log("取消更新");
					}
				})
			}

			function downWgt() {
				plus.nativeUI.showWaiting("下载wgt文件...");
				console.log(mDownloadUrl + mFileName); //
				plus.downloader.createDownload(mDownloadUrl + mFileName, {
					filename: "_doc/update/"
				}, function(d, status) {
					if(status == 200) {
						console.log("下载wgt成功：" + d.filename);
						installWgt(d.filename); // 安装wgt包
					} else {
						console.log("下载wgt失败！");
						plus.nativeUI.alert("下载wgt失败！");
					}
					plus.nativeUI.closeWaiting();
				}).start();
			}

			function installWgt(path) {
				plus.nativeUI.showWaiting("安装wgt文件...");
				//path : _doc/update/20180110104106K96jD4KX(3).png
				plus.runtime.install(path, {}, function() {
					plus.nativeUI.closeWaiting();
					plus.nativeUI.alert("应用资源更新完成！", function() {
						plus.runtime.restart();
					});
				}, function(e) {
					plus.nativeUI.closeWaiting();
					plus.nativeUI.alert("安装wgt文件失败[" + e.code + "]：" + e.message);
				});
			}
		</script>
	</body>

</html>